package com.jqb.webservice.handler;

import java.util.Set;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
/*客户端Handler*/
public class MixServiceHandler implements SOAPHandler<SOAPMessageContext>{
	//服务网址
//	private String webUrl = "http://localhost:9999/service?wsdl";
	//服务命名空间[所有QName必须加上此命名空间]
	private String serviceNs = "http://impl.service.webservice.jqb.com/";
	//服务名称
//	private String serviceName = "MixServiceImplService";
	@Override
	public boolean handleMessage(SOAPMessageContext context) {
		Boolean out = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
		if(out){
			SOAPMessage soapMsg = context.getMessage();
			try {
				SOAPEnvelope soapEnvelope = soapMsg.getSOAPPart().getEnvelope();
				SOAPHeader soapHeader = soapEnvelope.getHeader();
				SOAPBody soapBody = soapEnvelope.getBody();
				//获取要执行的方法名称,只对login()和addUser()方法添加头部信息
				String method = soapBody.getChildNodes().item(0).getLocalName();
				if("login".equals(method)||"addUser".equals(method)){
					if(soapHeader==null){
						soapHeader = soapEnvelope.addHeader();
					}
					QName qname = new QName(serviceNs,"licenceInfo","sx");
					soapHeader.addHeaderElement(qname).setValue("licenInfo");	
				}
				
				
			} catch (SOAPException e) {
				e.printStackTrace();
			}
		}
		return true;
	}

	@Override
	public boolean handleFault(SOAPMessageContext context) {
		System.out.println("err");
		return false;
	}

	@Override
	public void close(MessageContext context) {
		
	}

	@Override
	public Set<QName> getHeaders() {
		return null;
	}

}
